<?php
namespace App\Model;

use PhalApi\Model\NotORMModel as NotORM;

class Music extends NotORM {
	/* 音乐分类 */
	public function getCLass() {
		
		$list=\PhalApi\DI()->notorm->music_class
				->select('id,title,thumb')
				->order('list_order asc')
				->fetchAll();
		return $list;
	}
    
    /* 音乐列表 */
	public function getMusicList($where='',$p,$order='uses desc') {
        
        if($p<1){
            $p=1;
        }
		$pnum=50;
		$start=($p-1)*$pnum;
        
		
		$list=\PhalApi\DI()->notorm->music
                    ->select('id,title,author,thumb,length,href')
                    ->where('isdel=0')
                    ->where($where)
                    ->order($order)
                    ->limit($start,$pnum)
                    ->fetchAll();

		return $list;
	}

    /* 是否收藏 */
	public function isCollection($uid,$musicid) {
		
		$isexist=\PhalApi\DI()->notorm->music_collection
				->select('id')
				->where('uid=? and musicid=?',$uid,$musicid)
				->fetchOne();
        if($isexist){
            return '1';
        }
        
		return '0';
	}
	
    /* 我的收藏 */
	public function getCollection($uid) {
        		
		$list=\PhalApi\DI()->notorm->music_collection
                    ->select('musicid')
                    ->where('uid=?',$uid)
                    ->order('id desc')
                    ->fetchAll();

		return $list;
	}
    
    /* 收藏 */
	public function setCollection($uid,$musicid) {

        $data=[
            'uid'=>$uid,
            'musicid'=>$musicid,
            'addtime'=>time(),
        ];
        \PhalApi\DI()->notorm->music_collection->insert($data);
        
		return '0';
	}
    
    /* 取消收藏 */
	public function cancelCollection($uid,$musicid) {
		
		$rs=\PhalApi\DI()->notorm->music_collection
				->where('uid=? and musicid=?',$uid,$musicid)
				->delete();

		return $rs;
	}

    /* 更新使用量 */
	public function upUses($musicid,$num) {
		
		$rs=\PhalApi\DI()->notorm->music
				->where('id=?',$musicid)
				->update( array('uses' => new \NotORM_Literal("uses + {$num}") ));

		return $rs;
	}
}
